package isquirrel.servlets;

import isquirrel.beans.User;
import isquirrel.servlets.handler.HttpDBServlet;
import isquirrel.util.HibernateUtil;
import isquirrel.util.Utilities;

import java.io.IOException;

import javax.servlet.ServletException;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Servlet implementation class EditUserProfile
 */
public class EditUserProfile extends HttpDBServlet {
	private static final long serialVersionUID = 1L;
      	

    public EditUserProfile() {
        super();
    }

	@Override
	protected void handleGet() throws ServletException, IOException,
			JSONException {
		
	}

	@Override
	protected void handlePost() throws ServletException, IOException,
			JSONException {
		boolean error = false;
		int ID = -1;
		//System.out.println("lala2");
		if(request.getParameter("type")!=null){
			ID = Integer.parseInt(request.getParameter("type"));
		}
		JSONObject jsonResponse = new JSONObject();
		User u = getLoggedInUser();
		switch(ID){
		case 0:
			{
				String oldPassword = request.getParameter("old_password");
				if (Utilities.hashPassword(oldPassword).equals(u.getPassword())) {
					String password = request.getParameter("new_password");
					if (!password.equals("") && !request.getParameter("confirm_new_password").equals("")) {
						if (password.equals(request.getParameter("confirm_new_password"))) {
							u.setPassword(password);
						}
						else {
							jsonResponse.put("confirm_new_password", "The new passwords do not match");
							error = true;
						}
					}
					else {
						error = true;
						if (password.equals("")) {
							jsonResponse.put("new_password", "Pst, you forgot to write your new password");
						}
						if (request.getParameter("confirm_new_password").equals("")) {							
							jsonResponse.put("confirm_new_password", "Pst, you forgot to confirm your new password");
						}
					}
				} else {
					jsonResponse.put("old_password", "The password you just entered is not valid.");
					error = true;
				}
				break;
			}
		case 1:
			{
				String name = request.getParameter("name");
				if (name.equals("")) {
					error = true;
					jsonResponse.put("name", "Pst, you forgot to write your name");
				}else {
					u.setFirstName(name);
				}
				break;
			}
		case 2:
			{
				String surname = request.getParameter("surname");
				if (surname.equals("")) {
					error = true;
					jsonResponse.put("surname", "Pst, you forgot to write a surname");
				}else {
					u.setLastName(surname);
				}
				break;
			}
		case 3:
			{
				String day = request.getParameter("day");
				String month = request.getParameter("month");
				String year = request.getParameter("year");
				u.setDob(Utilities.convertStringDatetoDateObject(day+"/"+month+"/"+year, "dd/MM/yyyy"));
				break;
			}
		case 4:
			{
				String sex = request.getParameter("sex");
				u.setSex(sex);
				break;
			}
		}
		
		HibernateUtil.getSession().saveOrUpdate(u);
		if (error) 
			writeAjaxResponse(jsonResponse);
		else
			writeAjaxResponse("OK");
//			response.getWriter().print("OK");
	}		

}


